package com.java复习.jdbc.游戏.demo3;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class ArticleDao extends BaseDao{


    public static Article select(int id){
        Connection connection = getConnection();
        Article article = null;
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        try {
            preparedStatement = connection.prepareStatement("select * from article where id=?");
            preparedStatement.setInt(1,id);
            rs  = preparedStatement.executeQuery();
            while(rs.next()){
                 article = new Article(rs.getInt(1),
                        rs.getString(2),
                        rs.getInt(3),
                        rs.getInt(4),
                        rs.getInt(5),
                        rs.getInt(6),rs.getInt(7),rs.getInt(8));

            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }finally {
            close(connection,preparedStatement,rs);
        }
        return  article;
    }

    public ArrayList<Article> selectid(int id){
        Connection connection = getConnection();
       ArrayList<Article> arrayList = new ArrayList<Article>();
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        try {
            preparedStatement = connection.prepareStatement("select * from article where user_id=?");
            preparedStatement.setInt(1,id);
            rs  = preparedStatement.executeQuery();
            while(rs.next()){
                Article article = new Article(rs.getInt(1),
                        rs.getString(2),
                        rs.getInt(3),
                        rs.getInt(4),
                        rs.getInt(5),
                        rs.getInt(6),rs.getInt(7),rs.getInt(8));
                arrayList.add(article);
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }finally {
            close(connection,preparedStatement,rs);
        }
        return  arrayList;
    }
}
